package com.test.listener;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;

/**
 * 消费监听
 */
@Slf4j
public class RocketmqTestListener implements MessageListener {

    private Logger logger = LoggerFactory.getLogger(RocketmqTestListener.class);


    @Override
    public Action consume(Message message, ConsumeContext context) {
        try {
            byte[] body = message.getBody();
            String msg = new String(body);//获取到接收的消息，由于接收到的是byte数组，所以需要转换成字符串

            //TODO 业务逻辑，自行设计
            //testDao.insertDatas();//持久层，这里不再展述，自行补全
            System.out.println("===========================");
            log.info("接收到消息：{}",msg);
            System.out.println("===========================");
        } catch (Exception e) {
            logger.error("MessageListener.consume error:" + e.getMessage(), e);
            return Action.ReconsumeLater;
        }

        logger.info("MessageListener.Receive message");
        //如果想测试消息重投的功能,可以将Action.CommitMessage 替换成Action.ReconsumeLater
        return Action.CommitMessage;
    }

}